package com.zut.hospital.service.staff;

import com.alibaba.fastjson.JSON;
//import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import com.zut.hospital.dao.diagnose.PatientMapper;
import com.zut.hospital.dao.pharmacy.MedicineMapper;
import com.zut.hospital.dao.pharmacy.PurchaseMapper;
import com.zut.hospital.dao.pharmacy.Sale_RecordMapper;
import com.zut.hospital.dao.staff.OfficeMapper;
import com.zut.hospital.entity.diagnose.Patient;
import com.zut.hospital.entity.diagnose.PatientExample;
import com.zut.hospital.entity.pharmacy.*;
import com.zut.hospital.entity.staff.Office;
import com.zut.hospital.entity.staff.OfficeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author hyx
 * @date 2019/7/6 19:47
 * @description TODO
 * @Version
 */
@Service
public class PatServiceImpl implements PatService {
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    OfficeMapper officeMapper;
    @Autowired
    Sale_RecordMapper sale_recordMapper;
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    MedicineMapper medicineMapper;


    public String layuiJsonParse(List <Map <String, String>> list, int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }
    @Override
    public String dateUtil(Date start, Date end)throws java.text.ParseException {
        List <String> list = new ArrayList <String>();
        long s = start.getTime();
        long e = end.getTime();

        Long oneDay = 1000 * 60 * 60 * 24l;

        while (s <= e) {
            start = new Date(s);
            list.add(new SimpleDateFormat("yyyy-MM-dd").format(start));
            s += oneDay;
        }
        int count=0;
        int size=0;
        List<Map<String, String>> resultList = new ArrayList<>();
        Map<String,String>map;
       for (String s1:list){

           map=new HashMap<>();
            count=countPatient(new SimpleDateFormat("yyyy-MM-dd").parse(s1));
            map.put("time",s1);
            map.put("count",String.valueOf(count));
           resultList.add(map);
       }
       size=resultList.size();
       return layuiJsonParse(resultList,size);
    }

    @Override
    public int countPatient(Date time) {
        String t=new SimpleDateFormat("yyyy-MM-dd").format(time);
        List<Patient>patientList=patientMapper.selectByTime(t);
        System.out.println("patientList.size()"+patientList.size());
        int count=0;
        for(int i=0;i<patientList.size();i++){
            count++;
        }
        return count;

    }

    @Override
    public String date(Date start, Date end) throws ParseException {
        List <String> list = new ArrayList <String>();
        long s = start.getTime();
        long e = end.getTime();
        Long oneDay = 1000 * 60 * 60 * 24l;
        while (s <= e) {
            start = new Date(s);
            list.add(new SimpleDateFormat("yyyy-MM-dd").format(start));
            s += oneDay;
        }

        int size=0;
        int total=0;
        int totalp=0;
        List<Map<String, String>> resultList = new ArrayList<>();
        Map<String,String>map;
        for (String s1:list){

            map=new HashMap<>();
            total=countSale(new SimpleDateFormat("yyyy-MM-dd").parse(s1));
            totalp=countPur(new SimpleDateFormat("yyyy-MM-dd").parse(s1));
            map.put("time",s1);
            map.put("total",String.valueOf(total));
            map.put("totalp",String.valueOf(totalp));

            resultList.add(map);
        }
        size=resultList.size();
        return layuiJsonParse(resultList,size);

    }

    @Override
    public int countSale(Date time) {
        String t=new SimpleDateFormat("yyyy-MM-dd").format(time);

        List<Sale_Record>sale_records=sale_recordMapper.selectByTime(t);
        System.out.println("sale_records.size()"+sale_records.size());

        int scount=0;
        int sprice=0;
        int total=0;
for (Sale_Record sale_record:sale_records){
    MedicineExample medicineExample=new MedicineExample();
    medicineExample.createCriteria().andIdEqualTo(sale_record.getSaleRecMdcId());
Medicine medicine=medicineMapper.selectByExample(medicineExample).get(0);
   // total=sale_record.getSaleRecCount()*sale_record.getSaleRecPrice()-medicine.getMdcRemark()
}
        for(int i=0;i<sale_records.size();i++){
            total=scount*sprice;
            total++;
        }
        return total;

    }

    @Override
    public int countPur(Date time) {
        String t=new SimpleDateFormat("yyyy-MM-dd").format(time);
        PurchaseExample purchaseExample=new PurchaseExample();
        List<Purchase>purchaseList=purchaseMapper.selectByExample(purchaseExample);

        System.out.println("purchaseList.size()"+purchaseList.size());

        int count=0;
        int price=0;
        int totalp=0;
        for(int i=0;i<purchaseList.size();i++){
            totalp=count*price;
            totalp++;
        }
        return totalp;

    }




}





